<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Ajax-PHP Rating Stars Script</title>
<style type="text/css">
pre {background:#fafbfe;margin:8px 10px 8px 25px; font-weiht:800; font-size:16px; color:#0001da;}
</style>
</head>
<body>
<center><h2>Ajax-PHP Rating Stars Script</h2>
<sup>from <a href="http://coursesweb.net/php-mysql/">coursesweb.net</a></sup></center><br />
This is a script to add a <strong>Rating System</strong> (with stars) to rate images, articles, videos, games, or any element in pages of your web site.<br />
This <b>Rating Stars Script</b> is made using Ajax technology, with JavaScript and PHP (with OOP), it can be included in any page (".html", or ".php"), but the server must run PHP.<br/>
The rating data for each element can be saved in TXT files on the server, or in a MySQL database. By default, the script saves data in TXT files.<br />
The rated item, the number of the day, and the user (name or IP) are saved on the server, and cookies, so, the user can rate again the next day.<br />
You can use the script to add rating system to one or more elements in a web page.<br />
If you have a register /login script in your web site, you can set this Rating Script to allow only the logged users to rate. By default, all the visitors can rate an element once to 24 hours.
<h4>Installation and Settings</h4>
1. Copy on your server the directories: "<b>ratingfiles</b>", and "<b>ratingtxt</b>" (with all their files), in the same folder where you have the files in which you want to include the script.<br />
2. In the &lt;head&gt; &lt;/head&gt; zone of the page in which you want to use this script, add the following 2 lines of code (for CSS styles, and JavaScript functions):
<pre>
&lt;link href=&quot;ratingfiles/ratings.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;script src=&quot;ratingfiles/ratings.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
</pre>
3. In the place /places of the web page where you want to display the stars and the rating system, add the following code (a DIV tag):
<pre>&lt;div class=&quot;srtgs&quot; id=&quot;rt_??&quot;&gt;&lt;/div&gt;</pre>
 - Replace the two "<b>??</b>"  characters with the name /ID of the element you want to be rated, the rating stars added by JavaScript in this DIV will be associated to this element. This name (or ID) must be unique, and always after the characters: "<b>rt_</b>" in the "id" attribute.<br/>
<i>For example, if you want to apply the rating system to an image, "img_name.jpg", and to an article, both in the same page, add under each one a DIV with the code presented above</i> The "id" attribute in each DIV will be: <b>id="rt_img_name"</b>, and <b>id="rt_article1"</b>. Similarly you can proceed with multiple elements.<br/>
&bull; You can see how it is added in the file "test.html" (which can be used for test, if it is accessed from the servve).
<p style="border:1px solid red;padding:2px;">If the script is used in multiple pages of your website, you should be aware to not have two identical names, even if they are in different pages, because they are saved in the same ".txt" file (or mysql table).</p>
4. The "<b>ratingtxt</b>" directory, with its TXT files: "<b>rtgitems.txt</b>", and "<b>rtgusers.txt</b>" are necesary only if you want to save the rating data on TXT files.<br/>
In this case you must set read-write-execute permissions to these files, CHMOD 0777 (or 0755) so the PHP can write data in these .txt files.<br /><br/>
<div style="background:#eeeffe;padding:3px;">
- If you want to save data in MySQL database, you can ignore /delete the "ratingtxt/" directory.<br/>
In this case, open the file "<b>ratings.php</b>" (in the "ratingfiles/" folder), and edit the following data:<br/> &nbsp; &nbsp;
 1) To set the script to use MySQL, change the value of the constant <b>SVRATING</b> from '<i>txt</i>' to '<b>mysql</b>' :
 <pre>define('SVRATING', 'mysql');</pre> &nbsp; &nbsp;
 2) For connecting to your MySQL database:
<pre>
define('DBHOST', 'localhost');            - replace <b>localhost</b> with your MySQL server address
define('DBUSER', 'root');                 - replace <b>root</b> with your database user name
define('DBPASS', 'passdb');               - replace <b>passdb</b> with your password for MySQL database
define('DBNAME', 'dbname');               - replace <b>dbname</b> with the name of your MySQL database
</pre>
 3) Create the tables in MySQL database. To create the tables, open in your browser the file "<b>ratingfiles/create_tables.php</b>" .<br/>
 (In your browser you must access an address like: "<i>http://localhost/ratingfiles/create_tables.php</i>"), "localhost" is the domain name.
</div>
<h4>Other settings</h4>
- If you want the user to can rate only a single item in one day, change the value of the constant <b>NRRTG</b> from 0 to 1.
<pre>define('NRRTG', 0);</pre><br/>
<div style="background:yellow;">
If your web site has a register /login script, and you want than only the logged users to can rate the elements, set the value of <b>USRRATE</b> to 0 . Then set <b>$_SESSION['username']</b> with the session your script uses to keep logged users; to the following code, in "ratings.php"
<pre>
define('USRRATE', 1);
if(USRRATE !== 1) {
  if(!isset($_SESSION)) session_start();
  if(isset($_SESSION['username'])) define('RATER', $_SESSION['username']);
}
</pre><br/>
</div><br/>
&bull; If the rating data are saved in MySQL database:<br/>
To create a list which ranks the score of multiple voted items, based on highest rating, perform a MySQL Select in the "rtgitems" table. This table stores the voted items (in "item" column), points (in "totalrate" column), and number of votes (in "nrrates" column).<br/>
Example:
<pre>SELECT `item`, (`totalrate` / `nrrates`) AS `rank` FROM `rtgitems` ORDER BY (`totalrate` / `nrrates`) DESC LIMIT 10</pre><br/>
&bull; If you want to use this Rating Script in pages that are in different folders, open the "ratings.js" file (in "ratingfiles" directory), and replace this code (line 115):<br/>
<pre style="background:yellow;">cerere_http.open("POST", 'ratingfiles/ratings.php', true);</pre>
With this line of code (to use the absolute path to the "ratings.php" file):
<pre>cerere_http.open("POST", 'http://domain_name/ratingfiles/ratings.php', true);</pre><br/><hr/>
&bull; In the archive "<b>5_stars_rating_script.zip</b>" is the same script, but with five numbers to rate (1, 2, 3, 4, 5).<br/><br/>
&bull; In the archive "<b>7_stars_rating_script.zip</b>" is the same script, but with 7 stars.<br/><br/>
&bull; In the archive "<b>10_stars_rating_script.zip</b>" is the same script, but with 10 stars.<br/><br/>
The script has been tested successfully on Mozilla Firefox, Internet Explorer, Google Chrome and Opera.<br/>
You can test it online on the page: <a href="http://coursesweb.net/php-mysql/rating-stars-script-ajax-php_s2" title="Ajax-PHP Rating Stars Script">Ajax-PHP Rating Stars Script</a>.<br/><br/>
- The JavaScript instructions, the names and sizes in CSS, and stars are related, if you change something without knowing exactly what it does, it may not display / work properly.<br/><br/>
<i>With respect,
CoursesWeb.net</i>
<br/>
</body>
</html>